package vip.bblog.cunadmin.modules.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.bblog.cunadmin.annotation.group.Default;
import vip.bblog.cunadmin.common.entity.BaseResult;
import vip.bblog.cunadmin.common.entity.PageResult;
import vip.bblog.cunadmin.modules.system.dto.ProjectParams;
import vip.bblog.cunadmin.modules.system.entity.*;
import vip.bblog.cunadmin.modules.system.service.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * <p>api</p>
 * @author yy
 */
@Api(tags = "api")
@ApiSort(1)
@RestController
@RequestMapping("/api")
public class ApiController {


    @Autowired
    private CardService cardService;
    @Autowired
    private CustomerService customerService;

    @ApiOperation(value = "获取权益卡信息")
    @GetMapping("getCardInfo")
    public BaseResult<List<Customer>> getCardInfo(ProjectParams params) {
        LambdaQueryWrapper<Customer> queryWrapper = Wrappers.lambdaQuery();
        //根据姓名
        queryWrapper.eq(Customer::getIdcard, params.getIdcard());
        // 身份证号
        queryWrapper.eq(Customer::getName, params.getName());
        List<Customer> list = customerService.list(queryWrapper);
        return BaseResult.success(list);
    }

    @ApiOperation(value = "获取激活卡信息")
    @GetMapping("getActiveCard")
    public BaseResult getActiveCard(ProjectParams params) {
        LambdaQueryWrapper<Card> queryWrapper = Wrappers.lambdaQuery();
        //根据卡号
        queryWrapper.eq(Card::getCardNum, params.getCardNum());
        // 激活码
        queryWrapper.eq(Card::getActivationNum, params.getActivationNum());
        // 已激活的
        queryWrapper.eq(Card::getIsActivation, 1);
        List<Card> list = cardService.list(queryWrapper);
        return BaseResult.success(list);
    }

    @ApiOperation(value = "根据卡号获取信息")
    @GetMapping("getCardInfoByCardNum")
    public BaseResult getCardInfoByCardNum(String cardNum) {
        Card card = cardService.getOne(Wrappers.<Card>lambdaQuery().eq(Card::getCardNum, cardNum));
        return BaseResult.success(card);
    }

    @ApiOperation(value = "提交客户个人信息")
    @PostMapping("saveCustomer")
    public BaseResult saveCustomer(@RequestBody Customer customer) {
        LambdaQueryWrapper<Card> queryWrapper = Wrappers.lambdaQuery();
        //根据卡号
        queryWrapper.eq(Card::getCardNum, customer.getCardNum());
        // 激活码
        queryWrapper.eq(Card::getActivationNum, customer.getCardPwd());
        Card card = cardService.getOne(queryWrapper);
        if(card == null){
            return BaseResult.build(300, "卡号或激活码错误");
        }else if(card.getIsActivation() == 1){
            return BaseResult.build(300, "该卡已被激活");
        }
        customer.setActivationTime(new Date());
        // 90天开始生效
        customer.setStartTime(addDays(new Date(), 90));
        // 根据年添加期限
        customer.setEndTime(addYears(new Date(), card.getTerm()));
        // 修改已激活状态
        card.setIsActivation(1);
        cardService.updateById(card);
        // 添加
        boolean save = customerService.save(customer);
        return BaseResult.success(customer);
    }

    @ApiOperation(value = "激活卡")
    @PostMapping("activeCard")
    public BaseResult activeCard(@RequestBody Customer customer) {
        LambdaQueryWrapper<Card> queryWrapper = Wrappers.lambdaQuery();
        //根据卡号
        queryWrapper.eq(Card::getCardNum, customer.getCardNum());
        // 激活码
        queryWrapper.eq(Card::getActivationNum, customer.getCardPwd());
        Card card = cardService.getOne(queryWrapper);
        if(card == null){
            return BaseResult.build(300, "卡号或激活码错误");
        }else if(card.getIsActivation() == 1){
            return BaseResult.build(300, "改卡已被激活");
        }
        card.setIsActivation(1);
        cardService.updateById(card);
        customer.setActivationTime(new Date());
        customer.setStartTime(new Date(90));
        customer.setEndTime(new Date(365+90));
        return BaseResult.success(customerService.updateById(customer));
    }

    public Date addDays(Date currentDate, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.DATE, days);
        // 输出加天数后的日期
        return calendar.getTime();
    }

    public Date addYears(Date currentDate, int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.YEAR, year);
        // 输出加天数后的日期
        return calendar.getTime();
    }
}

